package com.yytek.nmp.controller;

import java.util.List;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yytek.common.log.annotation.Log;
import com.yytek.common.log.enums.BusinessType;
import com.yytek.common.security.annotation.RequiresPermissions;
import com.yytek.nmp.domain.IotProductInfo;
import com.yytek.nmp.service.IIotProductInfoService;
import com.yytek.common.core.web.controller.BaseController;
import com.yytek.common.core.web.domain.AjaxResult;
import com.yytek.common.core.utils.poi.ExcelUtil;
import com.yytek.common.core.web.page.TableDataInfo;

/**
 * 产品Controller
 * 
 * @author yytek-iot
 * @date 2022-07-05
 */
@Api(value = "产品控制器", tags = {"产品管理"})
@RestController
@RequestMapping("/product")
public class IotProductInfoController extends BaseController
{
    @Autowired
    private IIotProductInfoService iotProductInfoService;
    /**
     * 查询所有产品列表
     */
    @ApiOperation("查询所有产品列表")
    @GetMapping("/allList")
    public AjaxResult allList(IotProductInfo iotProductInfo)
    {
        List<IotProductInfo> list = iotProductInfoService.selectIotProductInfoList(iotProductInfo);
        return AjaxResult.success(list);
    }

    /**
     * 分页查询产品列表
     */
    @ApiOperation("分页查询产品列表")
    @RequiresPermissions("nmp:product:list")
    @GetMapping("/list")
    public TableDataInfo list(IotProductInfo iotProductInfo)
    {
        startPage();
        List<IotProductInfo> list = iotProductInfoService.selectIotProductInfoList(iotProductInfo);
        return getDataTable(list);
    }

    /**
     * 导出产品列表
     */
    @ApiOperation("导出产品列表")
    @RequiresPermissions("nmp:product:export")
    @Log(title = "产品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotProductInfo iotProductInfo)
    {
        List<IotProductInfo> list = iotProductInfoService.selectIotProductInfoList(iotProductInfo);
        ExcelUtil<IotProductInfo> util = new ExcelUtil<IotProductInfo>(IotProductInfo.class);
        util.exportExcel(response, list, "产品数据");
    }

    /**
     * 获取产品详细信息
     */
    @ApiOperation("获取产品详细信息")
    @RequiresPermissions("nmp:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(iotProductInfoService.selectIotProductInfoById(id));
    }

    /**
     * 新增产品
     */
    @ApiOperation("新增产品")
    @RequiresPermissions("nmp:product:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody IotProductInfo iotProductInfo)
    {
        //判断产品唯一名称是否重复
        if(iotProductInfoService.existByProductKey(iotProductInfo.getProductKey())){
            return AjaxResult.error("产品唯一名称重复");
        }else {
            return toAjax(iotProductInfoService.insertIotProductInfo(iotProductInfo));
        }

    }

    /**
     * 修改产品
     */
    @ApiOperation("修改产品")
    @RequiresPermissions("nmp:product:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotProductInfo iotProductInfo)
    {
        //判断产品唯一名称是否重复
        if(iotProductInfoService.existByProductKeyAndId(iotProductInfo.getProductKey(),iotProductInfo.getId())){
            return AjaxResult.error("产品唯一名称重复");
        }else {
            return toAjax(iotProductInfoService.updateIotProductInfo(iotProductInfo));
        }
    }

    /**
     * 删除产品
     */
    @ApiOperation("删除产品")
    @RequiresPermissions("nmp:product:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(iotProductInfoService.deleteIotProductInfoByIds(ids));
    }
}
